package com.doangeometrydash.scene;

import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import com.doangeometrydash.base.BaseScene;
import com.doangeometrydash.manager.SceneManager;
import com.doangeometrydash.manager.SceneManager.SceneType;

public class OptionsMenuScene extends BaseScene implements IOnAreaTouchListener
{
	Text textAutoReplay;
	Text textMusic;
	Sprite checkMusic;
	Sprite checkAR;
	Sprite uncheckMusic;;
	Sprite uncheckAR;;

	@Override
	public void createScene()
	{
		textAutoReplay = new Text(240, 130, mResourcesManager.gameFont, "Auto replay", vbom);
		textMusic = new Text(240, 190, mResourcesManager.gameFont, "music", vbom);

		uncheckMusic = new Sprite(textMusic.getX() + textMusic.getWidth() / 2 + 20, textMusic.getY(), mResourcesManager.uncheck, vbom);
		uncheckAR = new Sprite(textAutoReplay.getX() + textAutoReplay.getWidth() / 2 + 20, textAutoReplay.getY(), mResourcesManager.uncheck, vbom);
		checkMusic = new Sprite(textMusic.getX() + textMusic.getWidth() / 2 + 20, textMusic.getY(), mResourcesManager.check, vbom);
		checkAR = new Sprite(textAutoReplay.getX() + textAutoReplay.getWidth() / 2 + 20, textAutoReplay.getY(), mResourcesManager.check, vbom);

		attachChild(new Sprite(240, 160, mResourcesManager.option_background_region, vbom));
		attachChild(uncheckAR);
		attachChild(uncheckMusic);
		attachChild(textMusic);
		attachChild(textAutoReplay);

		if (!mResourcesManager.muteOption)
		{
			attachChild(checkMusic);
		}
		if (mResourcesManager.retryOption)
		{
			attachChild(checkAR);
		}
		this.setOnAreaTouchListener(this);

		this.registerTouchArea(uncheckMusic);
		this.registerTouchArea(uncheckAR);

	}

	@Override
	public void onBackKeyPressed()
	{
		SceneManager.getInstance().loadMenuScene();
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneType.SCENE_OPTION;
	}

	@Override
	public void disposeScene()
	{
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{		
		if (pSceneTouchEvent.isActionDown())
		{
			if (pTouchArea.contains(uncheckMusic.getX(), uncheckMusic.getY()))
			{
				if (!mResourcesManager.muteOption)
				{
					checkMusic.detachSelf();
					mResourcesManager.muteOption = true;
					mEngine.getMusicManager().setMasterVolume(0.0f);
				}
				else
				{
					attachChild(checkMusic);
					mResourcesManager.muteOption = false;
					mEngine.getMusicManager().setMasterVolume(1.0f);
				}
			}

			if (pTouchArea.contains(uncheckAR.getX(), uncheckAR.getY()))
			{
				if (mResourcesManager.retryOption)
				{
					checkAR.detachSelf();
					mResourcesManager.retryOption = false;					
				}
				else
				{
					attachChild(checkAR);
					mResourcesManager.retryOption = true;					
				}
			}
		}

		return true;
	}
}
